package com.uk.ui.fatura;

import java.util.Date;

import com.uk.data.ejbs.IFaturaBean;
import com.uk.data.entities.Fatura;
import com.vaadin.data.Container;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.Table;
import com.vaadin.ui.TableFieldFactory;
import com.vaadin.ui.TextField;

public class LeximiInfo extends HorizontalLayout {

	
	
	
	final Double afrofe;
	
	public LeximiInfo(IFaturaBean faturaBean){
		
		setSpacing(true);

		afrofe = faturaBean.getAfrofe();
		final Table table = new Table();
		final Table tab = new Table();
		tab.setPageLength(1);
		final IndexedContainer ic1 = new IndexedContainer();
		ic1.addContainerProperty("Harxhimi", String.class, "");
		final Object id2 = ic1.addItem();
		ic1.getContainerProperty(id2, "Harxhimi").setValue("-");
		tab.setContainerDataSource(ic1);
		
		tab.setColumnWidth("Harxhimi", 250);
		table.setPageLength(2);
		table.setSortDisabled(true);
		tab.setSortDisabled(true);
		final IndexedContainer ic = new IndexedContainer();
		ic.addContainerProperty("Periudha", String.class, "");
		ic.addContainerProperty("Data", Date.class, null);
		ic.addContainerProperty("Afrofe", Boolean.class, false);
		ic.addContainerProperty("Leximi", String.class, "");
		
		
		final Object id = ic.addItem();
		ic.getContainerProperty(id, "Periudha").setValue("Muaji i kaluar");
		ic.getContainerProperty(id, "Data").setValue(new Date());
		
		final Object id1 = ic.addItem();
		ic.getContainerProperty(id1, "Periudha").setValue("Muaji aktual");
		ic.getContainerProperty(id1, "Data").setValue(new Date());
		
		
		
		table.setContainerDataSource(ic);
		table.setTableFieldFactory(new TableFieldFactory() {
			
			@Override
			public Field createField(Container container, Object itemId,
					Object propertyId, Component uiContext) {
					final Object itId = itemId;
					if (propertyId.toString().equals("Data")) {
						return new PopupDateField();
					}
					if (propertyId.toString().equals("Afrofe")) {
						final CheckBox ch =  new CheckBox();
						ch.setImmediate(true);
						ch.addListener(new Property.ValueChangeListener() {
							
							@Override
							public void valueChange(ValueChangeEvent event) {
								Boolean b = (Boolean)ch.getValue();
								if (b.booleanValue()){
									if (itId == id)
										ic.getContainerProperty(id, "Leximi").setValue(afrofe);
									if (itId == id1) {
										ic.getContainerProperty(id1, "Leximi").setValue(afrofe);
									}	
								}
								else {
									
									if (itId == id)
										ic.getContainerProperty(id, "Leximi").setValue("");
									if (itId == id1) {
										ic.getContainerProperty(id1, "Leximi").setValue("");
									}
								}
								
							}
						});
						return ch;
					}
					if (propertyId.toString().equals("Leximi")) {
						final TextField tf = new TextField();
						tf.setImmediate(true);
						tf.addListener(new Property.ValueChangeListener() {
							
							@Override
							public void valueChange(ValueChangeEvent event) {
								
								try {
									Double h1 = new Double(ic.getContainerProperty(id, "Leximi").getValue().toString());
									Double h2 = new Double(ic.getContainerProperty(id1, "Leximi").getValue().toString());
									ic1.getContainerProperty(id2, "Harxhimi").setValue(h2 - h1);
								} catch (Exception e) {
									ic1.getContainerProperty(id2, "Harxhimi").setValue("Vlera te pasakta");
								}
																	
							}
						});
						return tf;
					}
				return null;
			}
		});
		table.setEditable(true);
		this.addComponent(table);
		this.addComponent(tab);
		
		
		
		
	}
}
